﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Portal.Cms.Bussiness;
using Portal.Web.Utility;
using Portal.Cms.Model;
using Portal.Utility;
using Portal.Web.Model;
using Portal.Core;
using dtSearch32Lib;
using DocumentSearchServiceModel;
using DocumentSearch32Lib;
using Portal.Systems.Model;
using Portal.Workflow.Bussiness;
using Portal.Cms.Utility;
using System.Data;
using Portal.Workflow.Render;
using Portal.Workflow.Utilities;
using System.Data.SqlClient;
using System.Collections.ObjectModel;

namespace Portal.Cms.Render
{
    public class PreviewVersionCmsDataRenderBll
    {
        class TabItemCls
        {
            public string Alias;
            public string Subject;
            public TabItemCls()
            {
            }
            public TabItemCls(string _Alias, string _Subject)
            {
                Alias = _Alias;
                Subject = _Subject;
            }
        }

        static string GetPlugInJavascript(string SiteId)
        {
            string ScriptText =
                    " <script language=javascript>\r\n" +
                    "     function ClientStartWorkflow(CmsDataId)\r\n" +
                    "     {\r\n" +
                    "         SiteId='" + SiteId + "';\r\n" +
                    "         AjaxOut = Portal.Cms.Render.PreviewVersionCmsDataRenderBll.ServerSideCheckStartWorkflow(SiteId,CmsDataId).value;\r\n" +
                    "         if(AjaxOut.Error)\r\n" +
                    "         {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "         }\r\n" +

                    "         if(confirm('Bạn đã chắc chắn khởi tạo quy trình phát hành bộ bản tin chưa?')==false)return;\r\n" +
                    "         AjaxOut = Portal.Cms.Render.PreviewVersionCmsDataRenderBll.ServerSideStartWorkflow(SiteId,CmsDataId).value;\r\n" +
                    "         if(AjaxOut.Error)\r\n" +
                    "         {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "         }\r\n" +
                    "         alert(AjaxOut.InfoMessage);\r\n" +
                    "     }\r\n" +


                    "     function PreviewVersionCmsData(DataSaveHistoryId,CmsDataId)\r\n" +
                    "     {\r\n" +
                    "         SiteId='" + SiteId + "';\r\n" +
                    "         AjaxOut = Portal.Cms.Render.PreviewVersionCmsDataRenderBll.ServerPreviewVersionCmsData(SiteId,DataSaveHistoryId,CmsDataId).value;\r\n" +
                    "         if(AjaxOut.Error)\r\n" +
                    "         {\r\n" +
                    "           alert(AjaxOut.InfoMessage);\r\n" +
                    "           return;\r\n" +
                    "         }\r\n" +

                    "     }\r\n" +
                    " </script>\r\n" +
                    PlugInWorkflowRender.GetJavascript(SiteId, "content_workflow", "divToolBar", true);
                  
            return ScriptText;
        }

       


        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string GroupDataId = (string)WebEnvironments.Request(SiteId, "gid");
                string CmsDataId = (string)WebEnvironments.Request(SiteId, "CmsDataId");
                string HistoryDataId = (string)WebEnvironments.Request(SiteId, "HistoryDataId");
                string Back = (string)WebEnvironments.Request(SiteId, "Back");
                
                string Html =
                   GetPlugInJavascript(SiteId) +
                   "<div id=\"content_workflow\"></div>\r\n"+
                   "<div id=\"divPreviewForm\">\r\n" +
                        DrawPreviewForm(SiteId, GroupDataId, CmsDataId, HistoryDataId, string.Empty, Back).HtmlContent +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }
        public static string BaseSiteUrl
        {
            get
            {
                HttpContext context = HttpContext.Current;
                string baseUrl = context.Request.Url.Authority + context.Request.ApplicationPath.TrimEnd('/');
                return baseUrl;
            }
        }

        public static string GetSelect(string SiteId, string FileName, string ComboboxId, string FocusId)
        {
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string Xml = OSiteParam.PathRoot + "\\Admin\\XmlData\\News\\" + FileName;
                DataSet ds = new DataSet();
                ds.ReadXml(Xml);
                for (int iIndex = 0; iIndex < ds.Tables[0].Rows.Count; iIndex++)
                {
                    string Id = (string)ds.Tables[0].Rows[iIndex]["Id"];
                    string Name = (string)ds.Tables[0].Rows[iIndex]["Name"];
                    if (FocusId.Equals(Id))
                    {
                        return Name;
                    }
                }
                return "";
            }
            catch
            {
                return "";
            }
        }

        static bool Check(string[] Items, string Id)
        {
            for (int iIndex = 0; iIndex < Items.Length; iIndex++)
            {
                if (Items[iIndex].Trim().Equals(Id.Trim()))
                {
                    return true;
                }
            }
            return false;
        }



        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerPreviewVersionCmsData(
            string SiteId,
            string DataSaveHistoryId,
            string CmsDataId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                DataSaveHistoryCls[] DataSaveHistories = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingDataSaveHistoryDetail(SiteId, DataSaveHistoryId, CmsDataId);

            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;

        }
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut BuildCategory(
            string SiteId, 
            string DataLanguage,
            string CategoryIds, 
            string HotCategoryIds)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string[] ItemCategoryIds = CategoryIds.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                string[] ItemHotCategoryIds = HotCategoryIds.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                
                //string XmlFile = OSiteParam.PathRoot + "\\admin\\xmldata\\news\\Category\\"+DataLanguage+"\\Category.xml";
                //DataSet ds = new DataSet();
                //ds.ReadXml(XmlFile);
                //DataView dv = new DataView(ds.Tables[0], "ParentId='0'", "", DataViewRowState.CurrentRows);
                
                GroupDataCategoryCls[] GroupDataCategories = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingGroupDataCategory(SiteId, DataLanguage);

                string Html = "";
                for (int iIndex = 0; iIndex < GroupDataCategories.Length; iIndex++)
                {
                    string Id = GroupDataCategories[iIndex].GroupDataCategoryId;
                    string Name = GroupDataCategories[iIndex].Name;

                    //DataView dvSub = new DataView(ds.Tables[0], "ParentId='" + Id + "'", "", DataViewRowState.CurrentRows);
                    if (GroupDataCategories[iIndex].SubGroupDataCategories.Count == 0)
                    {
                        bool bCheck = Check(ItemCategoryIds, Id);
                        if (bCheck)
                        {
                            bool bCheckHot = Check(ItemHotCategoryIds, Id);
                            string HotText = "";
                            if (bCheckHot) HotText = " (nóng)";
                            Html += Name +HotText+ "; ";
                        }
                    }
                    else
                    {
                        Html += ServerSideReadingSubField(SiteId,GroupDataCategories[iIndex].SubGroupDataCategories, DataLanguage, Id, ItemCategoryIds, ItemHotCategoryIds).HtmlContent;
                    }
                }
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReadingSubField(
            string SiteId,
            Collection<GroupDataCategoryCls> SubGroupDataCategories,
            string DataLanguage,
            string CategoryId,
            string[] ItemCategoryIds,
            string[] ItemHotCategoryIds)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                //string XmlFile = OSiteParam.PathRoot + "\\admin\\xmldata\\news\\Category\\" + DataLanguage + "\\Category.xml";
                //DataSet ds = new DataSet();
                //ds.ReadXml(XmlFile);
                //DataView dv = new DataView(ds.Tables[0], "ParentId='" + CategoryId + "'", "", DataViewRowState.CurrentRows);

                string Html = "";
                for (int jIndex = 0; jIndex < SubGroupDataCategories.Count; jIndex++)
                {
                    string Id = SubGroupDataCategories[jIndex].GroupDataCategoryId;
                    string Name = SubGroupDataCategories[jIndex].Name;
                    bool bCheck = Check(ItemCategoryIds, Id);
                    if (bCheck)
                    {
                        bool bCheckHot = Check(ItemHotCategoryIds, Id);
                        string HotText = "";
                        if (bCheckHot) HotText = " (nóng)";
                        Html = Html + Name +HotText+ "; ";
                    }
                }
                OAjaxOut.RetNumber = SubGroupDataCategories.Count;
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }




        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut DrawPreviewForm(string SiteId, string GroupDataId, string CmsDataId, string HistoryDataId, string Action, string Back)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, GroupDataId);
                DataSaveHistoryCls OCmsData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateCmsDataHistoryModel(null, SiteId, CmsDataId, HistoryDataId);
                if (OCmsData == null) throw new Exception("Tham số không hợp lệ! Truy cập bị cấm!");
                string WorkflowDefId = OCmsData.WorkflowDefId;
                if (string.IsNullOrEmpty(Action)) Action = "info";               

                string BackScript = "javascript:window.open('InputData.aspx?gid=" + OGroupData.GroupDataId + "','_self');";
                if (OGroupData.frkDataTypeId.Equals("document"))
                {
                    BackScript = "javascript:window.open('DocumentInputData.aspx?gid=" + OGroupData.GroupDataId + "','_self');";
                }
                if (!string.IsNullOrEmpty(Back))
                {
                    BackScript = "javascript:window.open('"+Back+"','_self');";
                }


                TabItemCls[] TabItems = new TabItemCls[]
                {
                    new TabItemCls("info","Thông tin chi tiết"),
                    //new TabItemCls("workflow","Lịch sử luân chuyển"),
                    //new TabItemCls("changehistory","Lịch sử thay đổi"),
                };

                //string UploadHandler = "CmsUploadHandler.ashx";
                //if (OGroupData.frkDataTypeId.Equals("document"))
                //{
                //    UploadHandler = "CmsUploadAttachFileHandler.ashx";
                //}
                StringBuilder Html = new StringBuilder();

                Html.Append(
                     "<script language=javascript>\r\n " +

                    "  function ProcessExpandCollapse(Id)\r\n" +
                    "  {\r\n" +
                    "     if(document.getElementById(Id).className=='Collapsed')\r\n" +
                    "     {\r\n" +
                    "         document.getElementById(Id).className='Expanded';\r\n" +
                    "     }\r\n" +
                    "     else\r\n" +
                    "     {\r\n" +
                    "         document.getElementById(Id).className='Collapsed';\r\n" +
                    "     }\r\n" +
                    "  }\r\n" +
                     
                     "  </script>\r\n");

                //string CodeCaption = "Số văn bản";
                //string DescCaption = "Mô tả";
                //string AttachCaption = "Ảnh gắn kèm";
                //if (OGroupData.frkDataTypeId.Equals("document"))
                //{
                //    CodeCaption = "Số văn bản";
                    //DescCaption = "Mô tả / trích yếu";
                    //AttachCaption = "Tệp toàn văn";
                //}
                string SelectVoteType = "";
                //if (OGroupData.frkDataTypeId.Equals("vote"))
                //{
                //    SelectVoteType = " <select id=drpSelectVoteType>\r\n" +
                //                     "      <option value=\"one\">Chỉ chọn 1 phương án</option>\r\n" +
                //                     "      <option value=\"two\">Chọn nhiều phương án</option>\r\n" +
                //                     "      <option value=\"text\">Nhập text</option>\r\n" +
                //                     " </select>\r\n";
                //}

                //string SelectIsNew = "bình thường";
                //if (OCmsData.IsNew == 1)
                //{
                //    SelectIsNew = "Mới";
                //}

                string SelectSearchable = "Không tìm kiếm toàn văn được";
                if (OCmsData.AttachedSearchable == 1)
                {
                    SelectSearchable = "Tìm kiếm toàn văn được";
                }

                string ProcessBy = "";
                string ButtonRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                bool ActiveWorkflow = !string.IsNullOrEmpty(WorkflowDefId);
                
                //Html.Append(
                   //" <input id=txtCheckedCategory type=hidden value=\"" + OCmsData.CategoryIds + "\">\r\n" +
                   //" <input id=txtCheckedHotCategory type=hidden value=\"" + OCmsData.HotCategoryIds + "\">\r\n" +
                   //" <div id=\"divToolBar\">\r\n"+
                   //" <div class=DatabaseFunction>\r\n" +
                   //"        <ul>\r\n");
                //if (ActiveWorkflow)
                //{
                //    bool HasStarted = WorkflowUtilities.CheckHasStartWorkflow(SiteId, CmsDataId);
                //    //if (HasStarted == false)
                //    //{
                //    //    Html +=
                //    //        "   <li onclick=\"javascript:ClientStartWorkflow('" + CmsDataId + "');\"><img class=image src=\"" + ButtonRoot + "/Workflow.png\"><div>Khởi tạo quy trình</div></li>\r\n";
                //    //}
                //    //else
                //    {
                //        Portal.Workflow.Bussiness.WorkflowBll.GetWorkflowCls
                //            OWorkFlowInfo = WorkflowBll.GetWorkflowActionInfo(OSiteParam.ConnectionString, WorkflowDefId, CmsDataId);
                //        if (OWorkFlowInfo != null)
                //        {
                //            if (OWorkFlowInfo.IsViewer == false)
                //            {
                //                //Html +=
                //                //    "   <li onclick=\"javascript:ProcessWorkflow('" + OWorkFlowInfo.WorkflowDefId + "','" + OWorkFlowInfo.WorkflowActionId + "','" + CmsDataId + "');\"><img class=image src=\"" + ButtonRoot + "/Workflow.png\"><div>Xử lý quy trình</div></li>\r\n";

                //                Html.Append(
                //                    AjaxWorkflowTaskNextLink.GetLinkButton(SiteId, OWorkFlowInfo.WorkflowDefId, OWorkFlowInfo.WorkflowActionId, CmsDataId));
                //            }
                //            else
                //            {
                //                ProcessBy = "<div class=CssCurrentProcessBy>Đang xử lý bởi: " + OWorkFlowInfo.CurrentProcessFullName + "</div>\r\n";
                //            }
                //        }
                //    }
                //}
                //Html.Append(
                //   "            <li onclick=\"" + BackScript + "\"><img class=image src=\"" + ButtonRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                //   "        </ul>\r\n" +
                //   "  </div>\r\n" +
                //   "</div>\r\n" +
                Html.Append(
                   "<div class=tab>\r\n" +
                    "   <ul>\r\n");
                for (int iIndex = 0; iIndex < TabItems.Length; iIndex++)
                {
                    if (TabItems[iIndex].Alias.ToLower().Equals(Action.ToLower()))
                    {
                        Html.Append("    <li class=active>" + TabItems[iIndex].Subject + "</li>\r\n");
                    }
                    else
                    {
                        Html.Append( "    <li><a href=\"PreviewCmsData.aspx?gid="+GroupDataId+"&CmsDataId="+OCmsData.DataId+"&back="+Back+"&action=" + TabItems[iIndex].Alias + "\">" + TabItems[iIndex].Subject + "</a></li>\r\n");
                    }
                }
                Html.Append( "</ul>\r\n" +
                        "</div>\r\n" +


                   "<div class=CssFormSaveUpdate>\r\n" +
                   "  <input type=hidden id=txtDataId value=\"" + CmsDataId + "\">\r\n" +
                    //"  <h3>Thêm mới dữ liệu</h3>\r\n" +
                   "  <div id=divCallSaveAddDataProcessing class=CssProcessing></div>\r\n");
                Action = "info";
                if (Action.Equals("info"))
                {
                    Html.Append(
                   "  <table>\r\n");
                    if (!string.IsNullOrEmpty(ProcessBy))
                    {
                        Html.Append(
                           "    <tr>\r\n" +
                           "        <td class=c1></td>\r\n" +
                           "        <td class=c2>" + ProcessBy + "</td>\r\n" +
                           "    </tr>\r\n");
                    }
                    Html.Append(
                       //"    <tr class=\"CssRowAlias\">\r\n" +
                       //"        <td class=c1>Số văn bản:</td>\r\n" +
                       //"        <td class=c2>" + OCmsData.DataAlias + "</td>\r\n" +
                       //"    </tr>\r\n" +
                       "    <tr>\r\n" +
                       "        <td class=c1>Số văn bản:</td>\r\n" +
                       "        <td class=c2>" + OCmsData.Subject + "</td>\r\n" +
                       "    </tr>\r\n" +
                        "    <tr>\r\n" +
                       "        <td class=c1>Trích yếu:</td>\r\n" +
                       "        <td class=c2>" + OCmsData.Description + "</td>\r\n" +
                       "    </tr>\r\n" +
                        //"    <tr>\r\n" +
                        //"        <td class=c1>Phụ đề:</td>\r\n" +
                        //"        <td class=c2>" + OCmsData.OtherSubject + "</td>\r\n" +
                        //"    </tr>\r\n" +
                        //"    <tr>\r\n" +
                        //"        <td class=c1 style=\"vertical-align:top;\">" + DescCaption + ":</td>\r\n" +
                        //"        <td class=c2>\r\n" +
                        //                 OCmsData.Description +
                        //"        </td>\r\n" +
                        //"    </tr>\r\n" +
                       //"    <tr " + StyleDisp + ">\r\n" +
                       //"        <td class=c1 style=\"vertical-align:top;\">Nội dung:</td>\r\n" +
                       //"        <td class=c2>\r\n" +
                       //             OCmsData.Body +
                       //"        </td>\r\n" +
                       //"    </tr>\r\n" +
                        //"    <tr>\r\n" +
                        //"        <td class=c1>Nguồn / tác giả:</td>\r\n" +
                        //"        <td class=c2>" + OCmsData.Source + "</td>\r\n" +
                        //"    </tr>\r\n"+
                       //"    <tr class=\"CssRowUrl\">\r\n" +
                       //"        <td class=c1>Url:</td>\r\n" +
                       //"        <td class=c2>" + OCmsData.Url + "</td>\r\n" +
                       //"    </tr>\r\n" +
                       "    <tr>\r\n" +
                       "        <td class=c1>Ngày tạo:</td>\r\n" +
                       "        <td class=c2>" + OCmsData.EntryDate.ToString("dd/MM/yyyy") + "</td>\r\n" +
                       "    </tr>\r\n");
                       //"    <tr>\r\n" +
                       //"        <td class=c1>Ngày hết hạn:</td>\r\n" +
                       //"        <td class=c2>" + OCmsData.ExpiredDate + "</td>\r\n" +
                       //"    </tr>\r\n";
                       //"    <tr>\r\n" +
                       //"        <td class=c1>Từ khóa:</td>\r\n" +
                       //"        <td class=c2>" + OCmsData.Keyword + "</td>\r\n" +
                       //"    </tr>\r\n";
                    //if (OGroupData.frkDataTypeId.ToLower().Equals("news"))
                    //{
                    //    Html +=

                    //   "    <tr>\r\n" +
                    //   "        <td class=c1>Chuyên mục:</td>\r\n" +
                    //   "        <td class=c2>" + BuildCategory(SiteId, OCmsData.DataLanguage, OCmsData.CategoryIds, OCmsData.HotCategoryIds).HtmlContent + "</td>\r\n" +
                    //   "    </tr>\r\n" +



                    //   "    <tr>\r\n" +
                    //   "        <td class=c1>Loại tin bài:</td>\r\n" +
                    //   "        <td class=c2>" + GetSelect(SiteId, "Portal_vi_NewsType.xml", "drpSelectNewsType", OCmsData.NewsTypeId) + "</td>\r\n" +
                    //   "    </tr>\r\n" +
                    //   "    <tr>\r\n" +
                    //   "        <td class=c1>Định mức ảnh mới:</td>\r\n" +
                    //   "        <td class=c2>" + GetSelect(SiteId, "Portal_vi_ImageLevel.xml", "drpSelectImageLevel", OCmsData.ImageLevelId) + "</td>\r\n" +
                    //   "    </tr>\r\n" +
                    //   "    <tr>\r\n" +
                    //   "        <td class=c1>Số ảnh khai thác:</td>\r\n" +
                    //   "        <td class=c2>" + OCmsData.ExpImageCount + "</td>\r\n" +
                    //   "    </tr>\r\n" +
                    //   "    <tr>\r\n" +
                    //   "        <td class=c1>Thuộc sự kiện:</td>\r\n" +
                    //   "        <td class=c2>" + GetSelect(SiteId, "Portal_vi_Event.xml", "drpSelectEvent", OCmsData.EventId) + "</td>\r\n" +
                    //   "    </tr>\r\n" +
                    //   "    <tr>\r\n" +
                    //   "        <td class=c1>Định mức ảnh khai thác:</td>\r\n" +
                    //   "        <td class=c2>" + GetSelect(SiteId, "Portal_vi_Exploitation.xml", "drpSelectExploitation", OCmsData.ExpImageLevelId) + "</td>\r\n" +
                    //   "    </tr>\r\n" +
                    //   "    <tr>\r\n" +
                    //   "        <td class=c1>Mức tin bài:</td>\r\n" +
                    //   "        <td class=c2>" + GetSelect(SiteId, "Portal_vi_NewsLevel.xml", "drpSelectNewsLevel", OCmsData.NewsLevelId) + "</td>\r\n" +
                    //   "    </tr>\r\n" +
                    //   "    <tr>\r\n" +
                    //   "        <td class=c1></td>\r\n" +
                    //   "        <td class=c2><ul class=CssDivCheckBox><li>" + (OCmsData.IsFocus == 1 ? "[X] " : "[ ] ") + "Là tin nổi bật " + "</li><li>" + (OCmsData.IsShowAuthor == 1 ? "[X] " : "[ ] ") + "Hiện tên tác giả " + "</li><li>" + (OCmsData.IsAdv == 1 ? "[X] " : "[ ] ") + "Quảng cáo doanh nghiệp " + "</li></ul></td>\r\n" +
                    //   "    </tr>\r\n" +

                    //   "    <tr>\r\n" +
                    //   "        <td class=c1>Mã cuộc bình chọn:</td>\r\n" +
                    //   "        <td class=c2>" + OCmsData.SurveyCode + "</td>\r\n" +
                    //   "    </tr>\r\n";
                    //}
                    //Html+=
                       //"    <tr>\r\n" +
                       //"        <td class=c1>Ghi chú:</td>\r\n" +
                       //"        <td class=c2>" + OCmsData.Note + "</td>\r\n" +
                       //"    </tr>\r\n" +
                       //"    <tr class=\"CssRowUrlType\">\r\n" +
                       //"        <td class=c1>Kiểu link:</td>\r\n" +
                       //"        <td class=c2>" + OCmsData.Target + "</td>\r\n" +
                       //"    </tr>\r\n";
                    if (OGroupData.frkDataTypeId.Equals("document"))
                    {
                        string SelectDocumentType = DocTypeProcessBll.CreateDocTypeModel(SiteId, OCmsData.frkDocTypeId).DocTypeName;
                        //    "<select id=drpSelectDocType class=drplist>\r\n";
                        //DocTypeCls[] DocTypes = DocTypeProcessBll.ReadingDocType(SiteId, GroupDataId);
                        //for (int iIndex = 0; iIndex < DocTypes.Length; iIndex++)
                        //{
                        //    if (OCmsData.frkDocTypeId.Equals(DocTypes[iIndex].DocTypeId))
                        //    {
                        //        SelectDocumentType += "   <option selected value=\"" + DocTypes[iIndex].DocTypeId + "\">" + DocTypes[iIndex].DocTypeName + "</option>\r\n";
                        //    }
                        //    else
                        //    {
                        //        SelectDocumentType += "   <option value=\"" + DocTypes[iIndex].DocTypeId + "\">" + DocTypes[iIndex].DocTypeName + "</option>\r\n";
                        //    }
                        //}
                        //SelectDocumentType += "</select>\r\n";


                        string SelectDocField = DocFieldProcessBll.CreateDocFieldModel(SiteId, OCmsData.frkDocFieldId).DocFieldName;
                        //  "<select id=drpSelectDocField class=drplist>\r\n";
                        //DocFieldCls[] DocFields = DocFieldProcessBll.ReadingDocField(SiteId, GroupDataId);
                        //for (int iIndex = 0; iIndex < DocFields.Length; iIndex++)
                        //{
                        //    if (OCmsData.frkDocFieldId.Equals(DocFields[iIndex].DocFieldId))
                        //    {
                        //        SelectDocField += "   <option selected value=\"" + DocFields[iIndex].DocFieldId + "\">" + DocFields[iIndex].DocFieldName + "</option>\r\n";
                        //    }
                        //    else
                        //    {
                        //        SelectDocField += "   <option value=\"" + DocFields[iIndex].DocFieldId + "\">" + DocFields[iIndex].DocFieldName + "</option>\r\n";
                        //    }
                        //}
                        //SelectDocField += "</select>\r\n";

                        string SelectDocReleaseFrom = DocReleaseFromProcessBll.CreateDocReleaseFromModel(SiteId, OCmsData.frkDocReleaseFromId).DocReleaseFromName;
                        //    "<select id=drpSelectDocReleaseFrom class=drplist>\r\n";
                        //DocReleaseFromCls[] DocReleaseFroms = DocReleaseFromProcessBll.ReadingDocReleaseFrom(SiteId, GroupDataId);
                        //for (int iIndex = 0; iIndex < DocReleaseFroms.Length; iIndex++)
                        //{
                        //    if (DocReleaseFroms[iIndex].DocReleaseFromId.Equals(OCmsData.frkDocReleaseFromId))
                        //    {
                        //        SelectDocReleaseFrom += "   <option selected value=\"" + DocReleaseFroms[iIndex].DocReleaseFromId + "\">" + DocReleaseFroms[iIndex].DocReleaseFromName + "</option>\r\n";
                        //    }
                        //    else
                        //    {
                        //        SelectDocReleaseFrom += "   <option value=\"" + DocReleaseFroms[iIndex].DocReleaseFromId + "\">" + DocReleaseFroms[iIndex].DocReleaseFromName + "</option>\r\n";
                        //    }
                        //}
                        //SelectDocReleaseFrom += "</select>\r\n";

                        string SelectDocSigner = DocSignerProcessBll.CreateDocSignerModel(SiteId, OCmsData.frkDocSignerId).DocSignerName;
                        //  "<select id=drpSelectDocSigner class=drplist>\r\n";
                        //DocSignerCls[] DocSigners = DocSignerProcessBll.ReadingDocSigner(SiteId, GroupDataId);
                        
                        //for (int iIndex = 0; iIndex < DocSigners.Length; iIndex++)
                        //{
                        //    if (DocSigners[iIndex].DocSignerId.Equals(OCmsData.frkDocSignerId))
                        //    {
                        //        SelectDocSigner += "   <option selected value=\"" + DocSigners[iIndex].DocSignerId + "\">" + DocSigners[iIndex].DocSignerName + "</option>\r\n";
                        //    }
                        //    else
                        //    {
                        //        SelectDocSigner += "   <option value=\"" + DocSigners[iIndex].DocSignerId + "\">" + DocSigners[iIndex].DocSignerName + "</option>\r\n";
                        //    }
                        //}
                        //SelectDocSigner += "</select>\r\n";

                        string SelectStatus = "Hết hiệu lực";
                            //"<select id=drpSelectDocStatus class=drplist>\r\n" +
                            //"   <option value=\"0\">Hết hiệu lực</option>\r\n" +
                            //"   <option value=\"1\">Có hiệu lực</option>\r\n" +
                            //"</select>\r\n";
                        if (OCmsData.DocStatus.Equals("1"))
                        {
                            SelectStatus = "Có hiệu lực";
                            //"<select id=drpSelectDocStatus class=drplist>\r\n" +
                            //"   <option value=\"0\">Hết hiệu lực</option>\r\n" +
                            //"   <option selected value=\"1\">Có hiệu lực</option>\r\n" +
                            //"</select>\r\n";
                        }

                        Html.Append(
                         "    <tr>\r\n" +
                         "        <td class=c1>Loại văn bản:</td>\r\n" +
                         "        <td class=c2>" + SelectDocumentType + "</td>\r\n" +
                         "    </tr>\r\n" +
                         "    <tr>\r\n" +
                         "        <td class=c1>Cơ quan ban hành:</td>\r\n" +
                         "        <td class=c2>" + SelectDocReleaseFrom + "</td>\r\n" +
                         "    </tr>\r\n" +
                         "    <tr>\r\n" +
                         "        <td class=c1>Lĩnh vực:</td>\r\n" +
                         "        <td class=c2>" + SelectDocField + "</td>\r\n" +
                         "    </tr>\r\n" +
                         "    <tr>\r\n" +
                         "        <td class=c1>Người ký:</td>\r\n" +
                         "        <td class=c2>" + SelectDocSigner + "</td>\r\n" +
                         "    </tr>\r\n" +
                         "    <tr>\r\n" +
                         "        <td class=c1>Trạng thái:</td>\r\n" +
                         "        <td class=c2>" + SelectStatus + "</td>\r\n" +
                         "    </tr>\r\n" +
                            //"    <tr>\r\n" +
                            //"        <td class=c1>Ngày ký:</td>\r\n" +
                            //"        <td class=c2>" + LookUpUtilities.GenerateLookUpDate("txtSignDate") + "</td>\r\n" +
                            //"    </tr>\r\n" +
                         "    <tr>\r\n" +
                         "        <td class=c1>Ngày ban hành:</td>\r\n" +
                         "        <td class=c2>" );

                        if(!string.IsNullOrEmpty(OCmsData.ReleaseDate))
                            Html.Append(OCmsData.ReleaseDate);

                        Html.Append(
                        "</td>\r\n" +
                         "    </tr>\r\n" +

                          "    <tr>\r\n" +
                         "        <td class=c1>Ngày đăng công báo:</td>\r\n" +
                         "        <td class=c2>");
                        if(!string.IsNullOrEmpty(OCmsData.SignDate))
                         Html.Append(OCmsData.SignDate);
                        
                        Html.Append("</td>\r\n" +
                         "    </tr>\r\n" +

                         "    <tr>\r\n" +
                         "        <td class=c1>Ngày hiệu lực:</td>\r\n" +
                         "        <td class=c2>");
                             if(!string.IsNullOrEmpty(OCmsData.ReleaseDate))
                                Html.Append(OCmsData.EffectedDate);
                        Html.Append( "</td>\r\n" +
                         "    </tr>\r\n" +
                         "    <tr>\r\n" +
                         "        <td class=c1>Ngày hết hiệu lực:</td>\r\n" +
                         "        <td class=c2>" + (!string.IsNullOrEmpty(OCmsData.EndEffectedDate) ? OCmsData.EndEffectedDate : string.Empty) + "</td>\r\n" +
                         "    </tr>\r\n" +
                         "    <tr>\r\n" +
                         "        <td class=c1>Tìm kiếm toàn văn:</td>\r\n" +
                         "        <td class=c2>" + SelectSearchable + "</td>\r\n" +
                         "    </tr>\r\n" +
                        "    <tr>\r\n" +
                     "        <td class=c1>Tài liệu đính kèm:</td>\r\n" +
                     "        <td class=c2><a href=\"#\" onclick=\"window.open('" + OCmsData.GetAttachDocUrl(SiteId, WebConfig.HttpDataRoot(OSiteParam)) + "','_blank'); \">Xem tài liệu</td>\r\n" +
                     "    </tr>\r\n");
                    
                    }

                    //if (OGroupData.frkDataTypeId.Equals("vote"))
                    //{
                    //    Html +=
                    //        "    <tr>\r\n" +
                    //        "        <td class=c1>Loại đánh giá:</td>\r\n" +
                    //        "        <td class=c2>" + SelectVoteType + "</td>\r\n" +
                    //        "    </tr>\r\n";
                    //}
                    Html.Append(
                       //"    <tr class=\"CssRowSortIndex\">\r\n" +
                       //"        <td class=c1>Thứ tự:</td>\r\n" +
                       //"        <td class=c2>" + OCmsData.SortIndex + "</td>\r\n" +
                       //"    </tr>\r\n" +
                       //"    <tr class=\"CssRowStatus\">\r\n" +
                       //"        <td class=c1>Trạng thái:</td>\r\n" +
                       //"        <td class=c2>" + SelectIsNew + "</td>\r\n" +
                       //"    </tr>\r\n" +

                       "  </table>\r\n" +
                       "</div>\r\n");
                }
                if (Action.Equals("workflow"))
                {
                    Html.Append( DrawWorkflowHistory(SiteId, OCmsData.DataId).HtmlContent);
                }

                if (Action.Equals("changehistory"))
                {
                    Html.Append(DrawChangeHistory(SiteId, OCmsData.DataId).HtmlContent);
                }
                Html.Append(
                   "<div class=CssTop><a href=\"#top\">Về đầu</a></div>\r\n");


                OAjaxOut.HtmlContent = Html.ToString();
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
                OAjaxOut.Error = true;
            }

            return OAjaxOut;
        }


        public static AjaxOut DrawWorkflowHistory(string SiteId, string CmsDataId)
        {
            string Html = "";
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                AjaxOut AjaxOutHistory = WorkflowBll.ServerSideGetWorkflowHistory(SiteId, OSiteParam.ConnectionString, CmsDataId, true);
                if (AjaxOutHistory.RetNumber > 0)
                {
                    Html +=
                       "<div class=\"CssWorkflowHistory\">\r\n" +
                            AjaxOutHistory.HtmlContent +
                       "</div>\r\n";
                }
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        public static AjaxOut DrawChangeHistory(string SiteId, string CmsDataId)
        {
            string Html = "";
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                DataSaveHistoryCls[] DataSaveHistories = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingDataSaveHistory(SiteId, CmsDataId);
                Html += "<div class=tabcontent>\r\n" +
                      " <h3>Lịch sử thay đổi ("+DataSaveHistories.Length.ToString("#,##0")+")</h3>\r\n";
                
                Html += "<table>\r\n" +
                        "<tr>\r\n" +
                        "   <th>#</th>\r\n" +
                        "   <th>Tiêu đề</th>\r\n" +
                        "   <th>Người thay đổi</th>\r\n" +
                        "   <th>Ngày thay đổi</th>\r\n" +
                        "   <th></th>\r\n" +
                        "</tr>\r\n";
                for (int iIndex = 0; iIndex < DataSaveHistories.Length; iIndex++)
                {
                    Html+=
                        "<tr>\r\n" +
                        "   <td>"+(iIndex+1)+"</td>\r\n" +
                        "   <td>" + DataSaveHistories[iIndex].Subject + "</td>\r\n" +
                        "   <td>" + DataSaveHistories[iIndex].SaveRoleName+"<br>"+ DataSaveHistories[iIndex].SaveByFullName + "</td>\r\n" +
                        "   <td>" + DataSaveHistories[iIndex].SaveDate.ToString("dd/MM/yyyy HH:mm") + "</td>\r\n" +
                        "   <td class=\"tdButton\"><input class=\"button\" type=\"button\" value=\"Xem\" onclick=\"javascript:PreviewVersionCmsData('" + DataSaveHistories[iIndex].DataSaveHistoryId + "','" + DataSaveHistories[iIndex].DataId + "')\"></th>\r\n" +
                        "</tr>\r\n";
                }
                Html += "</table>\r\n" +
                        "</div>\r\n"; 
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


       
    }
}
